# -*- coding: utf-8 -*-
import json,os
from jinja2 import Markup
from flask import flash, url_for, redirect, request, current_app
from flask_admin.base import expose
from flask_admin.model.template import EndpointLinkRowAction, LinkRowAction
from flask_wtf import FlaskForm
from sqlalchemy import func
from wtforms import SelectMultipleField, ValidationError
from wtforms.validators import DataRequired
from flask_admin.actions import action
from flask_admin.helpers import (get_form_data, validate_form_on_submit,get_redirect_target, flash_errors)
from flask_admin.contrib.sqla import form, filters as sqla_filters, tools
from . import MyModelView
from . import AjaxQueryIndexLike
from adminApp.extensions import db, redis_store


class HardDiskView(MyModelView):
    can_create = False
    can_edit = False
    column_filters = ('machine.idc.name', 'machine.idc.mark', 'machine.id', 'machine.hostname', 'machine.man_mark',
                      'machine.aqy_device_id', 'machine.is_pve', 'machine.is_virtual', 'machine.is_centos',
                      'name', 'mountpoint', 'enclosure',
                      'slot', 'model_number', 'manufacturer', 'firmware_revision',
                      'serial', 'serial_no', 'guid', 'is_root_disk', 'wwn',
                      'read_write', 'temperature', 'percent')

    column_list = ('machine.idc.name', 'machine.idc.mark', 'machine.hostname', 'machine.man_mark', 'name', 'mountpoint', 'enclosure',
                   'slot', 'firmware_revision', 'is_root_disk', 'fstype', 'read_write', 'temperature',
                   'size', 'used', 'percent')
    # column_sortable_list = (
    #     'id', 'name', 'category.name', 'author.name', 'serial_state', 'words', 'last_updated', 'created')

    column_editable_list = ('remark', )

    column_labels = {
        'machine.idc.name': '机房名称',
        'machine.idc.mark': '机房分段',
        'machine.hostname': '主机名称',
        'machine.man_mark': '设备标识',
        'machine.id': '设备记录ID',
        'machine.is_pve': 'PVE',
        'machine.is_virtual': '虚拟机',
        'machine.is_centos': 'Centos',
        'name': '盘符',
        'mountpoint': '挂载',
        'enclosure': '盘柜',
        'slot': '槽位',
        'state': '状态',
        'model_number': '批号',
        'manufacturer': '品牌',
        'firmware_revision': '固件版本',
        'serial':'SN',
        'serial_no': 'SN',
        'guid': 'GUID',
        'is_root_disk': '系统盘',
        'read_write': '读写错误',
        'temperature': '温度',
        'size': '大小',
        'used': '已使用',
        'percent': '百分比',
    }
    column_details_exclude_list = ('id', )
    column_formatters = dict(
        created=lambda v, c, m, p: m.created.strftime("%Y-%m-%d %H:%M:%S"),
        updated=lambda v, c, m, p: m.updated.strftime("%Y-%m-%d %H:%M:%S") if m.updated else '',
        size=lambda v, c, m, p: "{}G".format(round(m.size/1024/1024/1024,2)) if m.size else '',
        used=lambda v, c, m, p: "{}G".format(round(m.used/1024/1024/1024,2)) if m.used else '',
        mountpoint=lambda v, c, m, p: "{}".format(list(json.loads(m.mountpoint).keys())) if m.mountpoint else '',
    )

    details_modal = True